package net.begincode.servlet.course;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;

import com.google.gson.Gson;

import net.begincode.bean.BcCourse;
import net.begincode.bean.BcUser;
import net.begincode.servlet.common.BaseServlet;

public class CourseServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(CourseServlet.class);
	
	/**
	 * 新增课程初始化
	 * */
	public void addCourseInit(HttpServletRequest request, HttpServletResponse response){
		this.selBaseCourseNav(request);//查询基础教程下导航
		try {
			
			request.getRequestDispatcher("/WEB-INF/manage/editCourse.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 新增教程保存
	 * @throws UnsupportedEncodingException 
	 * */
	public void saveCourse(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		BcCourse course = new BcCourse();
		
		
		
		//初始化表单参数
		course.setBc_navigation_id(Integer.valueOf(request.getParameter("navId")));
		course.setCourse_name(request.getParameter("courseName"));
		course.setIntroduction(request.getParameter("courseintro"));
		course.setKeynote(request.getParameter("coursekey"));
		//初始化其他信息
		course.setCheck_flag("1");
		course.setIs_begincode("1");
		course.setRecommend("1");
		Map<String,String> userMap = this.getCookieInfo(request, response);
		course.setBc_user_id(Integer.valueOf(userMap.get("bcUserId").toString()));
		String courseId = request.getParameter("courseId");
		int  returnValue = 0;
		if(StringUtils.isEmpty(courseId)){
			returnValue = this.courseService.addCourse(course);
		}else{
			course.setBc_course_id(Integer.valueOf(courseId));
			returnValue = this.courseService.updCourse(course);
		}
		if(returnValue > 0){
			request.setAttribute("msg", "保存成功");
		}else{
			request.setAttribute("bcCourse", course);
			request.setAttribute("msg", "保存失败");
		}
		try {
			this.selBaseCourseNav(request);//查询基础教程下导航
			request.getRequestDispatcher("/WEB-INF/manage/editCourse.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 根据课程标识查询章节
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void viewChapterByCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String courseId = (String)request.getAttribute("id");
		request.setAttribute("course", this.courseService.selCourseByIdCache(courseId));
		request.setAttribute("chapters", this.chapterService.selChapterByCouseIdCache(courseId));
		request.setAttribute("codes", this.codeService.selCodesByCourseIdCache(courseId, 10)); 
		request.setAttribute("downs", this.bcDownService.selDownByParamLimitNCache(courseId, this.BOOK, 10));
		request.getRequestDispatcher("/WEB-INF/page/course_view.jsp").forward(request, response);
	}
	
	/**
	 * 查找所有教程，
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void selAllCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("courses", this.courseService.selAllCourse());
		request.getRequestDispatcher("/WEB-INF/manage/courseManage.jsp").forward(request, response);
	}
	
	/**
	 * 修改初始化
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void updCourseInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		this.selBaseCourseNav(request);//查询基础教程下导航
		String courseId = request.getParameter("courseId");
		request.setAttribute("bcCourse", this.courseService.selCourseById(courseId));
		request.getRequestDispatcher("/WEB-INF/manage/editCourse.jsp").forward(request, response);
	}
 
}
